package com.lykj.llw.box.ui.activity.agent

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.GsonUtils
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.app.BaseApplication
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.bus.FlowBus
import com.lykj.lib_base.component.GlideApp
import com.lykj.lib_base.constants.BConstants
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.lib_base.internal.utils.ToastUtils
import com.lykj.lib_base.net.http.CustomResponseBodyObserver
import com.lykj.lib_base.net.http.RetrofitManager
import com.lykj.llw.box.data.bean.response.agent.AgentUserInfoResponse
import com.lykj.llw.box.databinding.ActAgentSettingBinding
import com.lykj.llw.box.ui.diafrg.BottomPicPickerDiaFrg
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody

/**
 * ================================================
 * 代理设置页面
 * ================================================
 *
 * @author gosha
 * @date 2023/12/4-17:47
 */
class AgentSettingActivity : BaseActivity() {
    lateinit var binding: ActAgentSettingBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActAgentSettingBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initUI()
        initData()
        setListener()
    }

    override fun onResume() {
        super.onResume()

        ImmersionBar.with(this)
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()

        obtainAgentUserInfo()
    }

    override fun initUI() {
        binding.titleBar.tvTitle.text = "个人中心"
        binding.titleBar.tvRightOption.visibility = View.GONE
    }

    //获取代理用户信息
    private fun obtainAgentUserInfo() {
        RetrofitManager.instance.obtainAgentInfo(object :
            CustomResponseBodyObserver<ResponseBody>() {
            override fun onSubscribe(d: Disposable) {
                addDisposables(this@AgentSettingActivity, d)
            }

            override fun specificHandle(responseBody: String) {
                val bean = GsonUtils.fromJson(responseBody, AgentUserInfoResponse::class.java)

                binding.tvNickName.text =
                    "${
                        if (bean.data.nickname.isNullOrEmpty()) "${
                            bean.data.tel.substring(
                                0,
                                3
                            )
                        }****${bean.data.tel.substring(7)}" else bean.data.nickname
                    }"

                binding.tvAccount.text =
                    "${
                        bean.data.tel.substring(
                            0,
                            3
                        )
                    }****${bean.data.tel.substring(7)}"

                if (!bean.data?.headPic.isNullOrBlank()) {
                    GlideApp.with(this@AgentSettingActivity)
                        .load(bean.data?.headPic)
                        .circleCrop()
                        .transition(DrawableTransitionOptions.withCrossFade())
                        .into(binding.ivAvatar)
                }
            }

            override fun handleException(ret: String, msg: String?) {
                msg?.let {
                    ToastUtils.showShort(this@AgentSettingActivity, it)
                }
            }
        })
    }

    //退出登录
    private fun logout() {
        BaseApplication.instance?.clearAgentUserInfo(false)
        ToastUtils.showShort(this@AgentSettingActivity, "退出登录成功")
        finish()

        FlowBus.with<String>(BConstants.BUS_KEY.AGENT_LOGOUT_SUCCESS).post(lifecycleScope, "")
    }

    override fun initData() {
        FlowBus.with<String>("UpdateAgentAvatar").register(this) {
            GlideApp.with(this@AgentSettingActivity)
                .load(it)
                .circleCrop()
                .transition(DrawableTransitionOptions.withCrossFade())
                .into(binding.ivAvatar)
        }
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger {
            finish()
        }

        binding.tvBtnLogout.clickWithTrigger {
            logout()
        }

        binding.lltAvatar.clickWithTrigger(300) {
            BottomPicPickerDiaFrg()
                .setUserType(1)
                .show(supportFragmentManager, "图片获取方式选择弹窗")
        }

        binding.lltNickName.clickWithTrigger(300) {
            startActivity(Intent(this@AgentSettingActivity, AgentNickNameEditActivity::class.java))
        }
    }

    override fun onDestroy() {
        removeDisposables(this)
        super.onDestroy()
    }
}